package com.nuliji.service.extend;


import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;

import com.nuliji.tools.AbstractService;
import com.nuliji.util.annotation.PayModuleCallback;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gaojie on 2017/11/13.
 */
@Service
public class TradeService extends AbstractService {

    private Map<String, PayModuleCallback> registerCallback;

    public TradeService(){
        registerCallback = new HashMap<>();
        registerCallback.put("my/recharge", (id, pay)->{
            return true;
        });
    }

    public Object pay(Long userId, String subject, String body, String module, Long moduleId, BigDecimal money, int toolsType, HttpServletRequest request){
        if(!registerCallback.containsKey("module"))
            throw new ParameterException("不支持的支付模块");

        return null;
    }

    private Object prepay(Long userId, Object pay){

        return payed(null);
    }

    public Object payed(Long payId){

        PayModuleCallback callback = registerCallback.get(null);
        try {
            boolean r = callback.callback(null, null);
            if (!r)
                throw new SystemException("支付回调失败");
        }catch (Exception e){
            throw new SystemException("支付回调失败", e);
        }
        return null;
    }

}
